package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Cart;
import csu.webcourse.mypetstore.domain.Item;
import csu.webcourse.mypetstore.service.CartService;
import csu.webcourse.mypetstore.service.CategoryService;
import csu.webcourse.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

public class CartFormServlet extends HttpServlet {
    private static final String CART_FORM = "/WEB-INF/jsp/cart/cart.jsp";

    private CartService cartService;
    private CategoryService categoryService;
    private static ArrayList<String> ItemIds = new ArrayList<>();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        cartService = new CartService();

        //根据userid取出该用户购物车信息
        String userid = req.getParameter("userId");

        if (userid != null && !userid.equals("")) {

            ////若用户已登录，将未登录时添加的购物车项加入数据库
            for (int i = 0; i < ItemIds.size(); i++) {
                cartService.insertCartList(ItemIds.get(i), userid);
            }
            ItemIds.clear();
            AddItemToCartServlet.setItemIds(ItemIds);

            //通过userId信息获取Item对象
            ArrayList<String> ItemIds = cartService.getCart(userid);
            HttpSession session = req.getSession();
            session.removeAttribute("cart");//防止重复读取时数量变化
            for (int i = 0; i < ItemIds.size(); i++) {
                //生成Cart对象
                Cart cart = (Cart) session.getAttribute("cart");
                if (cart == null) {
                    cart = new Cart();
                }
                if (cartService.containsItemId(cart, ItemIds.get(i))) {
                    cartService.incrementQuantityByItemId(cart, ItemIds.get(i));
                } else {
                    categoryService = new CategoryService();
                    boolean isInStock = categoryService.isItemInStock(ItemIds.get(i));
                    Item item = categoryService.getItem(ItemIds.get(i));
                    cartService.addItem(cart, item, isInStock);
                }
                cart.setUserid(userid);
                //将Cart对象存入session
                session.setAttribute("cart", cart);
            }
            if (ItemIds.size() == 0) {
                Cart cart = new Cart();
                session.setAttribute("cart", cart);
            }
        } else {
            HttpSession session = req.getSession();
            Cart cart = (Cart) session.getAttribute("cart");
            if (cart == null) {
                cart = new Cart();
            }
            session.setAttribute("cart", cart);
        }

        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("loginAccount");
        if (account != null) {
            HttpServletRequest httpRequest = req;
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
            String logInfo = logService.logInfo(" ") + strBackUrl + "用户请求购物车界面";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        req.getRequestDispatcher(CART_FORM).forward(req, resp);
    }

    public static void setItemIds(ArrayList<String> itemIds) {
        ItemIds = itemIds;
    }
}
